
// 二级评论内容拼接到html
function pushCommentHtml(id,imgurl,uname,content,time) {
    var html = "<div class= 'comment-detail col-lg-12 col-md-12 col-sm-12 col-xs-12'>" +
    "           <div class='comment-head'>" +
    "               <div class='media-left'>" +
    "                   <a>" +
    "                       <img class='ui avatar image img-comment' src='" + imgurl + "'>" +
    "                   </a>" +
    "               </div>" +
    "               <div class='media-body'>" +
    "                   <span class='media-heading'>" + uname + "</span>" +
    "                   <span class='float-right'>" + time + "</span>"+
    "               </div><br>" +
    "           </div>" +
    "           <div class='comment-body'>" + content + "</div><br>" +
    "           </div>"

    var ele = $("#comment-" + id);
    //在html下面拼接 ele.append(html);
    //在html上面拼接
    ele.prepend(html);
}

// 查看二级评论
function showChildComment(ele) {
    var parentId =$(ele).attr("data-id");
    $(ele).toggleClass("operateBgColor");
    // noinspection JSJQueryEfficiency
    var childComment = $("#comment-" + parentId);
    var data = {
        commentId : parentId
    };

    if($("#comment-" + parentId).css("display") == 'none'){//如果show是隐藏的
        $("#comment-" + parentId).css("display","block");//show的display属性设置为block（显示）
    }else{//如果show是显示的
        $("#comment-" + parentId).css("display","none");//show的display属性设置为none（隐藏）
    }

    //控制二级评论div显示还是隐藏
    childComment.toggleClass("in ");
    //如果二级评论框显示，开始请求数据
    if(childComment.hasClass("in")){
        $.ajax({
            type:"GET",
            url:"http://localhost:8081/childComment",
            data:data,
            dataType:"json",
            contentType:"application/json",
            success:function (result) {
                var obj = result.obj;
                $.each(obj,function (i, o) {
                    pushCommentHtml(parentId, o.user.userProfilePhoto, o.user.userName, o.commentContent,o.commentDate);
                    // var time = new Date(o.commentDate).format("yyyy-MM-dd HH:mm:ss");
                });
            }
        });
    }else{
        //防止评论框关闭，再次点击内容重复叠加，每次都清空二级评论
        $("#comment-"+ parentId + " .comment-detail").remove();
    }
}

//对评论进行子评论
function subComment(ele) {
    var articleId = $('#articleId').val();
    var parentId = $(ele).attr("data-parentId");
    var subcomment = $("#childComment-" + parentId).val();
    if(subcomment == undefined || subcomment == null || subcomment == ""){
        toastr.warning("评论内容不能为空");
        return;
    }

    var data = {
        articleId: articleId,
        parentCommentId:parentId,
        commentContent:subcomment
    };

    $.ajax({
        type:"POST",
        url:"http://localhost:8081/child/comment",
        data:JSON.stringify(data),
        dataType:"json",
        contentType:"application/json",
        success:function (result) {
            if(result.code == 200){
                obj = result.obj;
                $("#childComment-"+parentId).val("");
                // pushCommentHtml(parentId,obj.avatarUrl,obj.userName,subcomment,obj.commentDate);
                //伪评论数增加
                // var commentCount = $("#commentCount-"+parentId);
                // var commentCountVal  = commentCount.text();
                // commentCountVal == '' ? commentCount.text(1) : commentCount.text(parseInt(commentCountVal)+1);
                toastr.success(result.message);
                setTimeout(function() {
                    window.location.reload();
                }, 2000)
            }else {
                toastr.error(result.message);
            }
        }
    });
}


//未登录时登录
function toLogin() {
    var path = window.location.href;
    window.localStorage.setItem("historyPath",path);
    window.location.href="/";

}

// 提交评论
function postComment() {
    var articleId = $('#articleId').val();
    var content = $('#commentArea').val();
    var json = {
        articleId: articleId,
        commentContent:content
    };
    if(content == undefined || content == ""){
        //alert("评论内容不能为空");
        toastr.warning("评论内容不能为空");
        return;
    }
    $.ajax({
        type:"POST",
        url:"http://localhost:8081/comment",
        data:JSON.stringify(json),
        dataType:"json",
        contentType:"application/json",
        success:function (result) {
            // 刷新页面
            toastr.success(result.message);
            setTimeout(function() {
                window.location.reload();
            }, 2000)
            // toastr.success(result.message,"",{
            //     onHidden:function () {
            //         window.location.reload();
            //     }
            // });
        }, error: function() {
            toastr.success(result.message);
        }
    });
}

// 增加文章收藏事件
function collect() {
    var userId = $('#userId').val();
    var articleId = $('#articleId').val();
    var json = {
        userId: userId,
        articleId: articleId
    };
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        url: "http://localhost:8081/article/collect",
        data: JSON.stringify(json),
        success: function(result) {
            // 刷新页面
            toastr.success(result.message);
            // alert("取消收藏成功");
            setTimeout(function() {
                window.location.reload();
            }, 1000)

        },
        error: function() {
            location.reload();
        }
    });
}

// 取消文章收藏事件
function cancel() {
    var userId = $('#userId').val();
    var articleId = $('#articleId').val();

    $.ajax({
        type: "DELETE",
        url: "http://localhost:8081/article/cancelCollect/" + userId + "/" + articleId,
        success: function(result) {
            // 刷新页面
            toastr.success(result.message);
            // alert("取消收藏成功");
            setTimeout(function() {
                window.location.reload();
            }, 1000)
        }
    });
}

// 增加评论点赞事件
function like(ele) {
    var userId = $('#userId').val();
    var commentId =$(ele).attr("like-id");
    var json = {
        userId: userId,
        commentId: commentId
    };
    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json;charset=utf-8",
        url: "http://localhost:8081/comment/like",
        data: JSON.stringify(json),
        success: function(result) {
            // 刷新页面
            toastr.success(result.message);
            // alert("取消收藏成功");
            setTimeout(function() {
                window.location.reload();
            }, 1000)

        },
        error: function() {
            location.reload();
        }
    });
}

// 取消评论点赞事件
function cancelLike(ele) {
    var userId = $('#userId').val();
    var commentId =$(ele).attr("like-id");

    $.ajax({
        type: "DELETE",
        url: "http://localhost:8081/comment/cancelLike/" + userId + "/" + commentId,
        success: function(result) {
            // 刷新页面
            toastr.success(result.message);
            // alert("取消收藏成功");
            setTimeout(function() {
                window.location.reload();
            }, 1000)
        }
    });
}